package jmetal.metaheuristics.singleObjective.island;

import java.util.Comparator;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.ForkJoinPool;

import jmetal.core.Algorithm;
import jmetal.core.Operator;
import jmetal.core.Problem;
import jmetal.core.Solution;
import jmetal.core.SolutionSet;
import jmetal.util.JMException;
import jmetal.util.comparators.ObjectiveComparator;

public class IslandFTGAController extends Algorithm {
	private static final int NUM_ISLANDS = 5;
	
	public static int evaluationsTotal;

	public IslandFTGAController(Problem problem) {
		super(problem);
		evaluationsTotal = 0;
	}

	@Override
	public SolutionSet execute() throws JMException, ClassNotFoundException {
		List<IslandFTGA> islands = new Vector<>();

		int populationSize;
		int maxEvaluations;
		int evaluations;

		Operator mutationOperator;
		Operator crossoverOperator;
		Operator selectionOperator;

		SolutionSet population;
		SolutionSet offspringPopulation;

		Comparator comparator;
		comparator = new ObjectiveComparator(0); // Single objective comparator

		// Read the params
		populationSize = ((Integer) this.getInputParameter("populationSize")).intValue();
		maxEvaluations = ((Integer) this.getInputParameter("maxEvaluations")).intValue();

		// Initialize the variables
		population = new SolutionSet(populationSize);
		offspringPopulation = new SolutionSet(populationSize);

		evaluations = 0;

		// Read the operators
		mutationOperator = this.operators_.get("mutation");
		crossoverOperator = this.operators_.get("crossover");
		selectionOperator = this.operators_.get("selection");

		// Create the initial population
		Solution newIndividual;
		for (int i = 0; i < populationSize; i++) {
			newIndividual = new Solution(problem_);
			problem_.evaluate(newIndividual);
			population.add(newIndividual);
		} // for

		// Sort population
		population.sort(comparator);
		int islandPopulationSize = populationSize / NUM_ISLANDS;
		int maxEpocas = 100;
		int maxEvaluationsIsland = (maxEvaluations / maxEpocas) / NUM_ISLANDS;

		SolutionSet imigrantesFilhos = new SolutionSet(islandPopulationSize);
		// Criando o Pool
		ForkJoinPool pool = new ForkJoinPool();
		SolutionSet imigrantes = new SolutionSet(islandPopulationSize);
		while (evaluations < maxEpocas) {
			islands.clear();
			imigrantes.clear();
			for (int i = 0; i < islandPopulationSize; i++) {
				imigrantes.add(population.get(i));
			}
			for (int i = 0; i < NUM_ISLANDS; i++) {
				islands.add(new IslandFTGA(islandPopulationSize, maxEvaluationsIsland, imigrantes, mutationOperator,
						crossoverOperator, selectionOperator, this, comparator));
			}
			for (IslandFTGA island : islands) {
				imigrantesFilhos = pool.invoke(island);
				for (int i = 0; i < imigrantesFilhos.size(); i++) {
					offspringPopulation.add(imigrantesFilhos.get(i));
				}
			}

			// The offspring population becomes the new current population
			Solution best = population.get(0);
			Solution best1 = population.get(1);
			population.clear();
			population.add(best);
			population.add(best1);
			for (int i = 0; i < populationSize - 2; i++) {
				population.add(offspringPopulation.get(i));
			}
			evaluations++;
			offspringPopulation.clear();
			population.sort(comparator);
		} // while

		// Return a population with the best individual
		SolutionSet resultPopulation = new SolutionSet(1);
		resultPopulation.add(population.get(0));
		return resultPopulation;
	}

}
